﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using ElectrosLtd.Models;
using Common.Views;
using ElectrosLtd.Models;
using Common.Views;
using System.Net.Mail;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html.simpleparser;
using System.Runtime.Remoting.Contexts;

namespace ElectrosLtd.Controllers
{
    public class HomeController : Controller
    {
        static int ProductID;
        public ActionResult MyFaults(int id)
        {
            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().GetAllFaultsForThisProduct(id, HttpContext.User.Identity.Name).ToList();
            ProductID = id;
            return View("MyFaults", FDV);
        }

        public ActionResult FilterFaults(string fromDate, string toDate)
        {
            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().GetFilteredFaultDetailsForUser(HttpContext.User.Identity.Name,fromDate, toDate, ProductID).ToList();
            return View("../Home/MyFaults", FDV);
        }
        
        public ActionResult About()
        {
            return View();
        }

        public ActionResult Index()
        {
            List<Product> products = new ProductsServ.WCFProductsClient().GetProducts().ToList();
            return View("Index", products);  
        }

        public ActionResult ProductDetails(int id)
        {
            ProductDetailsModel thisPM = new ProductDetailsModel();
            Product thisP = new ProductsServ.WCFProductsClient().GetProduct(id);
            decimal thisPR = new ProductsServ.WCFProductsClient().CalculateProductAvgRating(id);
                        
            int userRatings = new ProductsServ.WCFProductsClient().GetNoOfProductRates(id);
            int userComments = new ProductsServ.WCFProductsClient().GetNoOfProductComments(id);
            
            thisPM.ID = thisP.ID;
            thisPM.Name = thisP.Name;
            thisPM.Features = thisP.Features;
            thisPM.DateListed = thisP.DateListed;
            thisPM.Price = thisP.Price;
            thisPM.Stock = thisP.Stock;
            thisPM.Image = thisP.Image;
            
            thisPM.ProductRating = thisPR;
            thisPM.NoOfRatings = userRatings;
            thisPM.NoOfComments = userComments;
            return View(thisPM);
        }

        [Authorize]
        public ActionResult Add(int id, int quantity)
        {
            ShoppingCart sc = new ShoppingCart();
            sc.Username_FK = HttpContext.User.Identity.Name;
            sc.Product_FK = id;
            sc.Quantity = quantity;
            new UsersServ.WCFUsersClient().AddToCart(sc);
            return RedirectToAction("Index");
        }

        public ActionResult GenerateStatistics()
        {
            return View();
        }

        public ActionResult PlaceComment(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitComment(int pID, string Comment)
        {
            Comment newC = new Comment();
            newC.Username_FK = HttpContext.User.Identity.Name;
            newC.Product_FK = pID;
            newC.Comment1 = Comment;
            new UsersServ.WCFUsersClient().PlaceComment(newC);
            return View();
        }
                
        public ActionResult SearchProductByName(string name)
        {
            if (name != null)
            {
                List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProductsByName(name).ToList();
                return View("../Home/Index", ProductList); 
            }
            else
            {
                List<Product> ProductList = new ProductsServ.WCFProductsClient().GetProducts().ToList();
                return View("../Home/Index", ProductList);
            }
        }

        //For Faults

        public ActionResult UpdateFaults()
        {
            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().GetFaultDetailsForAdmin(HttpContext.User.Identity.Name).ToList();
            //List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().GetFaultDetailsForUser(HttpContext.User.Identity.Name).ToList();
            return View("UpdateFaults", FDV);
        }

        public ActionResult UpdateFaultDetails(int id)
        {
            UpdateFaultDetailsModel UFDM = new UpdateFaultDetailsModel();
            return View("UpdateFaultDetails",UFDM);
        }

        [HttpPost]
        public ActionResult UpdateFaultDetails(int id, UpdateFaultDetailsModel UFDM)
        {

            //Gets previous faults with the same token number
            UserFault prevUF = new UsersServ.WCFUsersClient().GetFaultByID(id);
            UserFault FirstUF = new UsersServ.WCFUsersClient().GetFaultsFirstDateByTokenNumber(prevUF.FaultID);

            UserFault newUF = new UserFault();

            newUF.FaultID = prevUF.FaultID;
            newUF.Username_FK = prevUF.Username_FK;
            newUF.Product_FK = prevUF.Product_FK;
            newUF.Details = prevUF.Details;
            newUF.Status_FK = UFDM.NewFaultID;
            newUF.Date = DateTime.Now;

            new UsersServ.WCFUsersClient().UpdateFault(newUF);
            User thisUser = new UsersServ.WCFUsersClient().GetUser(newUF.Username_FK);
            new MSGServ.WCFMessagingClient().SendSMS(thisUser.MobileNumber);

            

            //Creates PDF with all details

            MemoryStream ms = new MemoryStream();
            Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 30, 20);
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            {
                doc.Open();

                List<UserOrderDetailsView> userOD = new UsersServ.WCFUsersClient().GetOrderDetailsFromFaultNO(newUF.FaultID).ToList();
                List<FaultDetailsView> userFD = new UsersServ.WCFUsersClient().GetFaultDetailsForPDF(newUF.Username_FK, Convert.ToInt32(newUF.FaultID)).ToList();

                string pName = "";
                int Quantity = 0;
                double Price = 0;
                DateTime OrderDate = DateTime.Now;
                DateTime ExpiryDate = DateTime.Now;

                PdfPTable Toptable = new PdfPTable(2);
                PdfPCell Titlecell = new PdfPCell(new Phrase("Electros Ltd."));
                PdfPCell Datecell = new PdfPCell(new Phrase(FirstUF.Date.ToShortDateString() + " - " + newUF.Date.ToShortDateString()));

                Titlecell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                Datecell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;

                Titlecell.BorderWidthRight = 0;
                Datecell.BorderWidthLeft = 0;

                Toptable.AddCell(Titlecell);
                Toptable.AddCell(Datecell);

                doc.Add(Toptable);

                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable table = new PdfPTable(5);
                PdfPCell cell = new PdfPCell(new Phrase("Items: "));
                cell.BackgroundColor = Color.LIGHT_GRAY;
                cell.Colspan = 5;
                cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                table.AddCell(cell);
                table.AddCell("Item");
                table.AddCell("Amount Purchased");
                table.AddCell("Date Purchased");
                table.AddCell("Warranty Expiry Date");
                table.AddCell("Price");

                foreach (UserOrderDetailsView oudv in userOD)
                {
                    pName = oudv.ProductName;
                    Quantity = oudv.Quantity;
                    Price = Convert.ToDouble(oudv.Price) * Quantity;
                    OrderDate = oudv.ExpiryDate;
                    ExpiryDate = oudv.ExpiryDate.AddYears(2);

                    table.AddCell(pName);
                    table.AddCell(Quantity.ToString());
                    table.AddCell(OrderDate.ToShortDateString().ToString());
                    table.AddCell(ExpiryDate.ToShortDateString().ToString());
                    table.AddCell(Price.ToString());
                    break;
                }

                doc.Add(table);
                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable Faulttable = new PdfPTable(4);
                PdfPCell Faultcell = new PdfPCell(new Phrase("Faults: "));
                Faultcell.Colspan = 5;
                Faultcell.BackgroundColor = Color.LIGHT_GRAY;
                Faultcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                Faulttable.AddCell(Faultcell);
                Faulttable.AddCell("Ticket Number");
                Faulttable.AddCell("Date");
                Faulttable.AddCell("Details");
                Faulttable.AddCell("Status");

                int fID = 0;
                int ticketNo = 0;
                DateTime fDate = DateTime.Now;
                string status = "";
                string fDetails = "";

                foreach (FaultDetailsView fdv in userFD)
                {
                    ticketNo = fdv.TicketNo;
                    fDate = fdv.Date;
                    status = fdv.Status;
                    fDetails = fdv.FaultDetails;

                    Faulttable.AddCell(ticketNo.ToString());
                    Faulttable.AddCell(fDate.ToShortDateString().ToString());
                    Faulttable.AddCell(fDetails);
                    Faulttable.AddCell(status);
                }

                doc.Add(Faulttable);
                                
                doc.Add(Chunk.NEWLINE);
                                
                writer.CloseStream = false;
                doc.Close();
                ms.Position = 0;

            }

            //Sends Email
            MailMessage mm = new MailMessage();
            mm.To.Add(thisUser.Email);
            mm.From = new MailAddress("no-reply@ElectrosLtd.com");
            mm.Subject = "Fault Receipt";
            mm.Attachments.Add(new Attachment(ms, "FaultReceipt.pdf"));
            mm.Body = "Hi," + " " + thisUser.Firstname + " " + thisUser.Lastname + "<br/>";

            mm.Body += "A fault has been updated.";
            mm.Body += "<br/><br/>Happy shopping, <br/>Electros Ltd. Management";
            mm.Body += "<br/><br/>Copyright © 2014 Electros Ltd. All Rights Reserved. <br/> Designated trademarks and brands are the property of their respective owners. <br/> Electros Ltd and the Electros Ltd logo are trademarks of Electros Ltd.";
            mm.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential("tonixbox11@gmail.com", "proAnthony12");

            client.Send(mm);

            HttpContext.Response.Redirect("https://www.facebook.com/dialog/feed?app_id=298967543594233&redirect_uri=http%3A%2F%2Fwww.facebook.com&refid=0&to=706378750");

            return RedirectToAction("UpdateFaults", "Home");
        }

        //FOR ORDERS 

        [Authorize]
        public ActionResult MyOrders()
        {
            List<Order> userOrders = new UsersServ.WCFUsersClient().GetUserOrders(HttpContext.User.Identity.Name).ToList();
            return View("MyOrders", userOrders);
        }

        [Authorize]
        public ActionResult OrderDetails(Guid id)
        {
            List<UserOrderDetailsView> userOD = new UsersServ.WCFUsersClient().GetOrderDetailsByOrderID(id).ToList();

            for(int i=0; i< userOD.Count; i++)
            {
                userOD[i].ExpiryDate = userOD[i].ExpiryDate.AddYears(2);               
            }
            return View("OrderDetails", userOD);                
        }

        public ActionResult RateProduct(int id)
        {
            RatingsModel rm = new RatingsModel();
            rm.ProductID = id;
            return View(rm);
        }

        [HttpPost]
        public ActionResult RateProduct(RatingsModel rm)
        {
            decimal rating = rm.RatingID;

            Rating newR = new Rating();

            newR.Product_FK = rm.ProductID;
            newR.Rating1 = rating;
            newR.Username_FK = HttpContext.User.Identity.Name;

            new UsersServ.WCFUsersClient().PlaceRating(newR);
            return RedirectToAction("CommentPostedSuccessfully");
        }

        public ActionResult PostComment(int id)
        {
            CommentsModel cm = new CommentsModel();            
            return View(new CommentsModel());
        }

        [HttpPost]
        public ActionResult PostComment(int id, CommentsModel cm)
        {
            //new UsersServ.WCFUsersClient().
            Comment newC = new Comment();
            newC.Username_FK = HttpContext.User.Identity.Name;
            newC.Product_FK = id;
            newC.Comment1 = cm.Comment;          

            new UsersServ.WCFUsersClient().PlaceComment(newC);
            return RedirectToAction("CommentPostedSuccessfully");
        }

        //method to display that the post was successfully accepted
        public ActionResult CommentPostedSuccessfully()
        {
            return View();
        }

        public ActionResult OrderDetailsSentSuccessfully()
        {
            return View();
        }

        public ActionResult PrintOrderAndSend(Guid id)
        {
            List<UserOrderDetailsView> userOD = new UsersServ.WCFUsersClient().GetOrderDetailsByOrderID(id).ToList();
            User thisUser = new UsersServ.WCFUsersClient().GetUser(HttpContext.User.Identity.Name);

            //PDF 
            string contents = "";
            string date = DateTime.Now.ToShortDateString();
            date = date.Replace('/', '-');
            string guid = "";
            double total = 0;

            foreach (UserOrderDetailsView oudv in userOD)
            {
                guid = oudv.OID.ToString();
            }

            Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 30, 20);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("C:\\Users\\Anthony\\Desktop" + date + " " + guid + ".pdf", FileMode.Create));
            doc.Open();

            contents += "<h1>Electros Ltd. " + date + "</h1>";
            contents += "<table>";
            contents += "<tr><b><th>Item </th><th>Amount Purchased </th><th>Date Purchased </th><th>Warranty Expiry Date </th><th>Price </th></tr>";

            string pName = "";
            int Quantity = 0;
            decimal Price = 0;
            DateTime OrderDate = DateTime.Now;
            DateTime ExpiryDate = DateTime.Now;

            foreach (UserOrderDetailsView oudv in userOD)
            {
                pName = oudv.ProductName;
                Quantity = oudv.Quantity;
                Price = oudv.Price * Quantity;
                OrderDate = oudv.ExpiryDate;
                ExpiryDate = oudv.ExpiryDate.AddYears(2);

                contents += "<tr><td>" + pName + "</td><td>" + Quantity + " </td><td>" + OrderDate.ToShortDateString() + "</td><td>" + ExpiryDate.ToShortDateString() + " </td><td>" + Convert.ToDouble(Price * Quantity) + "</td></tr>";
                total += Convert.ToDouble(Price);
            }

            contents += "</table>";
            contents += "<br/>";

            //--------------------------Setting the Total Price ----------------------------------

            contents += "<table>";
            contents += "<tr><td>SubTotal : €" + Math.Round((total / 1.18), 2) + "</td></tr>";
            contents += "<tr><td>VatRate : 18%</td></tr>";
            contents += "<tr><td>Total : €" + total + "</td></tr>";
            contents += "</table>";

            var parseHTML = HTMLWorker.ParseToList(new StringReader(contents), null);
            foreach (var HTML in parseHTML)
            {
                doc.Add(HTML as IElement);
            }
            doc.Close();
            //System.Diagnostics.Process.Start("C:\\Users\\Anthony\\Desktop\\" + date + " " + guid + ".pdf");

            //Sends Email
            MailMessage mm = new MailMessage();
            mm.To.Add(thisUser.Email);
            mm.From = new MailAddress("no-reply@ElectrosLtd.com");
            //mm.Attachments.Add(new Attachment(doc));
            mm.Subject = "Order Receipt";

            mm.Body = "Hi," + " " + thisUser.Firstname + " " + thisUser.Lastname + "<br/><br/> You have purchased the following goods. <br/>";

            mm.Body += contents;
            mm.Body += "<br/><br/>Happy shopping, <br/>Electros Ltd. Management";
            mm.Body += "<br/><br/>Copyright © 2014 Electros Ltd. All Rights Reserved. <br/> Designated trademarks and brands are the property of their respective owners. <br/> Electros Ltd and the Electros Ltd logo are trademarks of Electros Ltd.";
            mm.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential("tonixbox11@gmail.com", "proAnthony12");

            client.Send(mm);
            return RedirectToAction("OrderDetailsSentSuccessfully");
        }

        [HttpGet]
        public ActionResult GetHighestRatedProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetTopRatedProducts();
            return PartialView("../Partial/GetHighestRatedProduct", sr);            
        }

        [HttpGet]
        public PartialViewResult GetMostPurchasedProducts()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetMostPurchasedProduct();
            return PartialView("../Partial/GetMostPurchasedProducts", sr);
        }

        [HttpGet]
        public PartialViewResult GetMostFaultsForProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetProductWithHighestNumberOfFaults();
            return PartialView("../Partial/GetMostFaultsForProduct", sr);
        }

        [HttpGet]
        public PartialViewResult GetLeastFaultsForProduct()
        {
            IEnumerable<StatisticalReportView> sr = new UsersServ.WCFUsersClient().GetProductWithLeastNumberOfFaults();
            return PartialView("../Partial/GetLeastFaultsForProduct", sr);
        }

        public ActionResult PrintStatistics()
        {
            string FD = DateTime.Now.ToShortDateString();
            string FT = DateTime.Now.ToShortTimeString();

            MemoryStream ms = new MemoryStream();
            Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 30, 20);
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            {
                doc.Open();



                PdfPTable Toptable = new PdfPTable(2);
                PdfPCell Titlecell = new PdfPCell(new Phrase("Electros Ltd."));
                PdfPCell Datecell = new PdfPCell(new Phrase(FD + " "+ FT));

                Titlecell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                Datecell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;

                Titlecell.BorderWidthRight = 0;
                Datecell.BorderWidthLeft = 0;

                Toptable.AddCell(Titlecell);
                Toptable.AddCell(Datecell);

                doc.Add(Toptable);

                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable PWHAR = new PdfPTable(3);
                PdfPCell PWHARcell = new PdfPCell(new Phrase("Products with highest average rating"));
                PWHARcell.Colspan = 3;
                PWHARcell.BackgroundColor = Color.LIGHT_GRAY;
                PWHARcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                PWHAR.AddCell(PWHARcell);
                PWHAR.AddCell("Product ID");
                PWHAR.AddCell("Product Name");
                PWHAR.AddCell("Average Rating");

                IEnumerable<StatisticalReportView> TRP = new UsersServ.WCFUsersClient().GetTopRatedProducts();
                IEnumerable<StatisticalReportView> TPP = new UsersServ.WCFUsersClient().GetMostPurchasedProduct();
                IEnumerable<StatisticalReportView> MPF = new UsersServ.WCFUsersClient().GetProductWithHighestNumberOfFaults();
                IEnumerable<StatisticalReportView> LPF = new UsersServ.WCFUsersClient().GetProductWithLeastNumberOfFaults();

                int pID = 0;
                string pName = "";
                decimal AVGRating;
                int mostP;
                int mostF;
                int leastF;

                foreach (StatisticalReportView thisSR in TRP)
                {
                    pID = thisSR.productID;
                    pName = thisSR.ProductName;
                    AVGRating = thisSR.mostRated;

                    PWHAR.AddCell(pID.ToString());
                    PWHAR.AddCell(pName);
                    PWHAR.AddCell(AVGRating.ToString());
                }

                doc.Add(PWHAR);
                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable MPP = new PdfPTable(3);
                PdfPCell MPPcell = new PdfPCell(new Phrase("Most purchased products "));
                MPPcell.Colspan = 3;
                MPPcell.BackgroundColor = Color.LIGHT_GRAY;
                MPPcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                MPP.AddCell(MPPcell);
                MPP.AddCell("Product ID");
                MPP.AddCell("Product Name");
                MPP.AddCell("Amount purchased");

                foreach (StatisticalReportView thisSR in TPP)
                {
                    pID = thisSR.productID;
                    pName = thisSR.ProductName;
                    mostP = thisSR.mostPurchased;

                    MPP.AddCell(pID.ToString());
                    MPP.AddCell(pName);
                    MPP.AddCell(mostP.ToString());
                }

                doc.Add(MPP);
                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable MPFT = new PdfPTable(3);
                PdfPCell MPFTcell = new PdfPCell(new Phrase("Products with most reported faults"));
                MPFTcell.Colspan = 3;
                MPFTcell.BackgroundColor = Color.LIGHT_GRAY;
                MPFTcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                MPFT.AddCell(MPFTcell);
                MPFT.AddCell("Product ID");
                MPFT.AddCell("Product Name");
                MPFT.AddCell("Reported Faults");

                foreach (StatisticalReportView thisSR in MPF)
                {
                    pID = thisSR.productID;
                    pName = thisSR.ProductName;
                    mostF = thisSR.mostFaults;

                    MPFT.AddCell(pID.ToString());
                    MPFT.AddCell(pName);
                    MPFT.AddCell(mostF.ToString());
                }

                doc.Add(MPFT);
                doc.Add(new Paragraph(20, "\u00a0"));

                PdfPTable LPFT = new PdfPTable(3);
                PdfPCell LPFTcell = new PdfPCell(new Phrase("Products with least reported faults"));
                LPFTcell.Colspan = 3;
                LPFTcell.BackgroundColor = Color.LIGHT_GRAY;
                LPFTcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                LPFT.AddCell(LPFTcell);
                LPFT.AddCell("Product ID");
                LPFT.AddCell("Product Name");
                LPFT.AddCell("Reported Faults");

                foreach (StatisticalReportView thisSR in LPF)
                {
                    pID = thisSR.productID;
                    pName = thisSR.ProductName;
                    leastF = thisSR.leastFaults;

                    LPFT.AddCell(pID.ToString());
                    LPFT.AddCell(pName);
                    LPFT.AddCell(leastF.ToString());
                }

                doc.Add(LPFT);

                writer.CloseStream = false;
                doc.Close();
                ms.Position = 0;
            }

            User thisUser = new UsersServ.WCFUsersClient().GetUser(HttpContext.User.Identity.Name);

            MailMessage mm = new MailMessage();
            mm.To.Add(thisUser.Email);
            mm.From = new MailAddress("no-reply@ElectrosLtd.com");
            mm.Attachments.Add(new Attachment(ms, "Statistics.pdf"));
            mm.Subject = "Statistical Report";

            mm.Body = "Hi," + " " + thisUser.Firstname + " " + thisUser.Lastname + "<br/><br/> You have requested a report. <br/>";


            mm.Body += "<br/><br/>Electros Ltd. Management";
            mm.Body += "<br/><br/>Copyright © 2014 Electros Ltd. All Rights Reserved. <br/> Designated trademarks and brands are the property of their respective owners. <br/> Electros Ltd and the Electros Ltd logo are trademarks of Electros Ltd.";
            mm.IsBodyHtml = true;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential("tonixbox11@gmail.com", "proAnthony12");

            client.Send(mm);


            return RedirectToAction("GenerateStatistics");
        }

        [HttpGet]
        public ActionResult FilterFaultsByDateForAdmin()
        {
            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().FilterFaultsByDateForAdmin(HttpContext.User.Identity.Name).ToList();
            return View("../Home/UpdateFaults", FDV);
        }

        public PartialViewResult _FilterFaultsForAdmin()
        {
            return PartialView(new FilterFaultsForAdminModel());
        }

        [HttpPost]
        public ActionResult AdvancedFF(string txtfD, string txttD, FilterFaultsForAdminModel FFFAM)
        {

            List<FaultDetailsView> FDV = new UsersServ.WCFUsersClient().AdvancedFaultSearch(FFFAM.usernameChosen, FFFAM.StatusChosen, txtfD, txttD).ToList();
            return View("../Home/UpdateFaults", FDV);
        }
    }
}
